// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Resources;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;



namespace QuoteAdvantage
{
	/// <summary>
	/// Minimal basic proposal generation as a simple HTML page that can be printed manually
	/// </summary>
   public class PrintQuote : System.Web.UI.Page
   {
		private Quote  objQuote = null;
      private string m_strQuoteId = "";
      protected string strTitleBar;
      private Engine eng = null;
      protected ResourceManager m_mgrResource;
      protected int nCurrencyFormatCode;
      protected string strCurrencySymbol;
      protected string strParentProductCustomHeader1 = "";
      protected string strParentProductCustomHeader2 = "";
      protected System.Web.UI.WebControls.Literal litPrintOutput;
      protected System.Web.UI.WebControls.LinkButton lnkPrint;
      protected System.Web.UI.WebControls.CheckBox chkShowPrices;
      protected System.Web.UI.WebControls.LinkButton lnkClose;

      private void Page_Load(object sender, System.EventArgs e)
      {  
         if (this.Trace.IsEnabled)
            this.Trace.Write("Starting Page Load event.");
         
         m_strQuoteId = Request.QueryString["QuoteId"];

         if (this.Trace.IsEnabled)
            this.Trace.Write("QuoteId=" + m_strQuoteId);

         // Set the culture and UI culture to the browser's accept language
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
         Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]);

         m_mgrResource = new ResourceManager("QuoteAdvantage.strings", typeof(CustomizeQuote).Assembly);

         if (this.Trace.IsEnabled)
            this.Trace.Write("Initializing Engine");
         eng = new Engine(this.Trace, m_mgrResource);

         objQuote = eng.LoadQuote(new Guid(m_strQuoteId));  
         Organization org = eng.LoadOrganization();
         strCurrencySymbol = org.CurrencySymbol;
         nCurrencyFormatCode = org.CurrencyFormatCode;
        
         if (!this.IsPostBack)
         {
            if (this.Trace.IsEnabled)
               this.Trace.Write("Initializing Page...");
            
            LoadResources();
            lnkClose.Attributes.Add("onclick", "return onCloseClicked()");
            lnkPrint.Attributes.Add("onclick", "return onPrintClicked()");

            if (this.Trace.IsEnabled)
               this.Trace.Write("Page Complete.");
         }

         GeneratePrintOutput();
      }

      #region Web Form Designer generated code
      override protected void OnInit(EventArgs e)
      {
         //
         // CODEGEN: This call is required by the ASP.NET Web Form Designer.
         //
         InitializeComponent();
         base.OnInit(e);
      }
		
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {    
         this.chkShowPrices.CheckedChanged += new System.EventHandler(this.chkShowPrices_CheckedChanged);
         this.Load += new System.EventHandler(this.Page_Load);

      }
      #endregion

      private void LoadResources() 
      {
      }
      
                             
      private void GeneratePrintOutput()
      {     
         decimal nTotal = 0;
         bool bShowParentCategoryNames = false;

         if (ConfigurationManager.AppSettings["ProposalShowParentProductCategoryNames"] != null)
         {
             bShowParentCategoryNames = Convert.ToBoolean(ConfigurationManager.AppSettings["ProposalShowParentProductCategoryNames"]);
         }

         strTitleBar = "Quote Number: " + objQuote.QuoteNumber;

         StringBuilder sb = new StringBuilder(1000);
         sb.Append("<br><br>");
         sb.Append("<span class=\"Proposal\">" + DateTime.Now.ToLongDateString() + "</span><br>");
         sb.Append("<table border=\"0\" class=\"Proposal\"><br>");
         sb.Append("<tr>");
         sb.Append("<td align=\"right\">Quote Name:</td><td>" + objQuote.Name + "</td>\r\n");
         sb.Append("</tr>\r\n");
         sb.Append("<tr>");
         sb.Append("<td align=\"right\">Number:</td><td>" + objQuote.QuoteNumber + "</td>\r\n");
         sb.Append("</tr>\r\n");
         sb.Append("</table><br>");
         
         sb.Append("<table border=\"0\" class=\"Proposal\">\r\n");
         sb.Append("<tr>");
         sb.Append("<td class=\"ProposalBold\">Item</td>\r\n");
         sb.Append("<td class=\"ProposalBold\">Qty</td>\r\n");
         sb.Append("<td class=\"ProposalBold\">Price</td>\r\n");
         sb.Append("</tr>");
         ArrayList objParentItemList = objQuote.GetConfigurableItems();
         XmlNode objCategoryList = eng.LoadProductCategoryList();
        
         foreach (QuoteDetail objItem in objParentItemList)
         {
            decimal nItemPrice = (objItem.Quantity * objItem.PricePerUnit) - objItem.ManualDiscountAmount;
            
            if (bShowParentCategoryNames)
            {      
               string strCategoryName = eng.GetCategoryPath(objItem.CategoryId, objCategoryList);

               sb.Append("<tr>");
               sb.Append(@"<td style=""font-weight:bold""> <br/>" + strCategoryName + "&nbsp;&nbsp;</td>\r\n");
               sb.Append("<td>&nbsp;</td>\r\n");
               sb.Append("</tr>\r\n");   
            }

            sb.Append("<tr>\r\n");
            sb.Append("<td>" + objItem.Name + "&nbsp;&nbsp;</td>\r\n");
            sb.Append("<td>" + objItem.Quantity + "&nbsp;&nbsp;</td>\r\n");
            sb.Append("<td>" + Product.FormatCurrency(nItemPrice, nCurrencyFormatCode, strCurrencySymbol) + "</td>\r\n");
            sb.Append("</tr>\r\n");   
                                  
            nTotal += nItemPrice;

            ArrayList objChildItemList = objItem.GetChildItemList();
            
            foreach (QuoteDetail objChild in objChildItemList)
            {                   
               decimal nLineItemPrice = DisplayLineItem(sb, objChild, 20);
               nTotal += nLineItemPrice;
            }

            sb.Append("<tr><td></td><td></td></tr>");
         }
         sb.Append("<tr><td></td><td></td></tr>");
         sb.Append("<tr><td></td><td align=\"right\">Total:</td><td>" + Product.FormatCurrency(nTotal, nCurrencyFormatCode, strCurrencySymbol) + "</td></tr>");
         sb.Append("</table><br><br><br>");
         litPrintOutput.Text = sb.ToString();
      }

      protected decimal DisplayLineItem(StringBuilder sb, QuoteDetail objParent, int nPadding)
      {
         decimal nTotal = 0;
         decimal nLineItemPrice = (objParent.Quantity * objParent.PricePerUnit) - objParent.ManualDiscountAmount;

         sb.Append("<tr>");
         sb.Append(@"<td style=""padding-left:" + nPadding + @"px"">" + objParent.Name + "&nbsp;&nbsp;</td>\r\n");
         sb.Append("<td>&nbsp;</td>\r\n");

         if (chkShowPrices.Checked)
         {
            sb.Append("<td>" + Product.FormatCurrency(nLineItemPrice, nCurrencyFormatCode, strCurrencySymbol) + "</td>\r\n");
         }
         else
         {
            sb.Append("<td>&nbsp;</td>\r\n");
         }
         sb.Append("</tr>");

         nTotal += nLineItemPrice;

         ArrayList objNestedChildItemList = objParent.GetChildItemList();
         
         foreach (QuoteDetail objItem in objNestedChildItemList)
         {
            nTotal += DisplayLineItem(sb, objItem, nPadding + 20);
         }

         return nTotal;
      }                    

      protected string CalculateItemPrice(object val)
      {
         QuoteDetail objItem = (QuoteDetail)val; 
         decimal  nPrice = objItem.CalculatePrice();

         string strResult = nPrice.ToString(".00");

         return strResult;
      }

      private void chkShowPrices_CheckedChanged(object sender, System.EventArgs e)
      {
      
      }
      
 
	}  // end class
}
